#!/bin/bash
###SHELLPACK preamble abinit-bench 8.10.2
###SHELLPACK parseargBegin
###SHELLPACK parseargParam --model      ABINIT_MODEL
###SHELLPACK parseargParam --processes  ABINIT_PROCESSES
###SHELLPACK parseargParam --threads    ABINIT_THREADS
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

export BLAS_VERSION="v0.3.10"
export SCALAPACK_MAKE_FLAGS="-DBUILD_SHARED_LIBS=on"
export SCALAPACK_VERSION="2.1.0"
export XC_VERSION="2.2.2"
export ATOMPAW_VERSION="4.0.0.13"
export FFTW_VERSION="3.3.7"

MPIRUN=
if [ "$ABINIT_OPENMPI_VERSION" != "" ]; then
	###SHELLPACK mpi_setup_deps ABINIT_OPENMPI_VERSION
	###SHELLPACK mpi_setup_env ABINIT_OPENMPI_VERSION ABINIT_MPI_PATH ABINIT_MPI_LIBPATH
	###SHELLPACK mpi_setup_shmem
	MPIRUN="mpirun $ABINIT_MPI_OPTS $MMTESTS_MPI_OPTS -np $ABINIT_PROCESSES"
	export OMP_NUM_THREADS=$ABINIT_THREADS
fi

###SHELLPACK check_external_install_required blasbuild      blasbuild-${BLAS_VERSION}           ${BLAS_VERSION}
###SHELLPACK check_external_install_required scalapackbuild scalapackbuild-${SCALAPACK_VERSION} ${SCALAPACK_VERSION}
###SHELLPACK check_external_install_required xcbuild        xcbuild-${XC_VERSION}               ${XC_VERSION}
###SHELLPACK check_external_install_required atompawbuild   atompawbuild-${ATOMPAW_VERSION}     ${ATOMPAW_VERSION}
###SHELLPACK check_external_install_required fftwbuild      fftwbuild-${FFTW_VERSION}           ${FFTW_VERSION}
export LD_LIBRARY_PATH="$SHELLPACK_SOURCES/abinitdeps-installed/$MMTESTS_LIBDIR:$SHELLPACK_SOURCES/abinitdeps-installed/$MMTESTS_LIBDIR:$LD_LIBRARY_PATH"
export PATH=$SHELLPACK_SOURCES/abinitdeps-installed/bin:$PATH
###SHELLPACK check_install_required_continue abinit-$VERSION

export ABI_HOME=$SHELLPACK_SOURCES/abinit-${VERSION}-installed
export PATH=$ABI_HOME/src/98_main/:$PATH
export ABI_TESTS=$ABI_HOME/tests/
export ABI_PSPDIR=$ABI_TESTS/Psps_for_tests/

case $ABINIT_MODEL in
tmbt)
	PREPARE_STAGE_MAP="1:tmbt_1.files::
2:tmbt_2.files:tmbt_1o_DS2_WFK:tmbt_2i_WFK"
	RUN_STAGE_MAP="3:tmbt_3.files:tmbt_1o_DS2_WFK:tmbt_3i_WFK"
	;;
*)
	die "Unrecognised model $ABINIT_MODEL"
esac

STEP=
FILES=
SOURCE_DATA=
STAGE_DATA=
prepare_stage() {
	STEP=`echo $STAGE | awk -F : '{print $1}'`
	FILES=`echo $STAGE | awk -F : '{print $2}'`
	SOURCE_DATA=`echo $STAGE | awk -F : '{print $3}'`
	STAGE_DATA=`echo $STAGE | awk -F : '{print $4}'`

	if [ "$SOURCE_DATA" != "" ]; then
		ln -s $SOURCE_DATA $STAGE_DATA || die "Failed to symbolic link $SOURCE_DATA $STAGE_DATA"
	fi
	cp ../$FILES . || die "Failed to copy $FILES"
	echo $FILES | sed -e 's/.files//' > $LOGDIR_RESULTS/stage-$STEP.name
}

cd $ABI_TESTS/tutoparal/Input/ || die "Failed to cd to $ABI_TESTS/tutoparal/Input/"
if [ "$INSTALL_ONLY" = "yes" ]; then
	rm -rf Work
	mkdir Work || die "Failed to create work directory"
	cd Work

	if [ "$ABINIT_PREPARE" = "yes" ]; then
		for STAGE in `echo $PREPARE_STAGE_MAP`; do
			STEP=`echo $STAGE | awk -F : '{print $1}'`
			prepare_stage
			echo Preparing $STAGE
			$MPIRUN abinit < ../$FILES > $ABINIT_MODEL-prepare.log.$STEP
		done
	fi
fi
###SHELLPACK init_complete
cd Work || die "Failed to change to work directory"
if [ "$ABINIT_PREPARE" = "yes" ]; then
	STAGE_MAP=$RUN_STAGE_MAP
else
	STAGE_MAP="$PREPARE_STAGE_MAP
$RUN_STAGE_MAP"
fi

echo $ABINIT_MAXCPUS > $LOGDIR_RESULTS/scaling-parameters
BENCH_CMD="$MPIRUN abinit"
log_runtime "OMP_NUM_THREADS=$ABINIT_THREADS"
log_runtime "OMP_PLACES=$OMP_PLACES"
log_runtime "OMP_PROC_BIND=$OMP_PROC_BIND"
log_cmd "$BENCH_CMD"
log_linkages $SHELLPACK_SOURCES/abinitdeps-installed/bin/abinit
for STAGE in `echo $STAGE_MAP`; do
	prepare_stage
	
	echo Executing $STAGE
	monitor_pre_hook $LOGDIR_RESULTS $ABINIT_MODEL-stage-$STEP
	eval $TIME_CMD -o $LOGDIR_RESULTS/abinit-time.$STEP	\
		$BENCH_CMD < ../$FILES > $ABINIT_MODEL-log.$STEP
	monitor_post_hook $LOGDIR_RESULTS $ABINIT_MODEL-stage-$STEP

	if [ $? -ne 0 ]; then
		die "Failed to execute stage $STEP of $ABINIT_MODEL"
	fi
	mv $ABINIT_MODEL-prepare.log.* $LOGDIR_RESULTS/
	mv $ABINIT_MODEL-log.* $LOGDIR_RESULTS
	gzip $LOGDIR_RESULTS/$ABINIT_MODEL-prepare.log.*
	gzip $LOGDIR_RESULTS/$ABINIT_MODEL-log.*
	for FILE in `ls *.out`; do
		mv $FILE $LOGDIR_RESULTS/$FILE
		gzip $LOGDIR_RESULTS/$FILE
	done
done

exit $SHELLPACK_SUCCESS
